<?
$iterator = new RecursiveIteratorIterator($this->container,  RecursiveIteratorIterator::SELF_FIRST);
$prevDepth = -1;
$count = $this->container->count();
?>
<div id="menu">
<?  foreach ($iterator as $page) :

    if ($page->ignore) {
        continue;
    }

    $depth = $iterator->getDepth();
    if ($depth < 1) {
        $count--;
    }

    if ($page->hidden) {
    	continue;
    }

    $isActive = $page->isActive(true);

    $linkClass = '';
    if ($page->hasPages()) {
       $linkClass = 'parent';
    }

    $ulClass = '';
    if ($prevDepth < 0) {
        $ulClass = 'menu';
    }

    $liClass = $isActive ? array('current') : array();
    if ($count == 0) {
        $liClass[] = 'last';
    }
    $liClass = implode(' ', $liClass);

    if ($depth > $prevDepth) {
        ?>
        <ul class="<?= $ulClass ?>">
        <?
    } else if ($prevDepth > $depth) {
        // close li/ul tags until we're at current depth
        for ($i = $prevDepth; $i > $depth; $i--) : ?>
             </li>
        </ul>
        <? endfor ?>

        </li>
        <?
    } else {
        ?>
        </li>
        <?
    }
    ?>

    <li class="<?= $liClass ?>"><a href="<?= $page->href ?>" class="<?= $linkClass ?>"><span><?= $page->title ?></span></a>
        
<?
    $prevDepth = $depth;

    endforeach;

    for ($i = $prevDepth + 1; $i > 0; $i--) {
        ?>
          </li></ul>
        <?
    }
?>
</div>